<template>
  <div class='container'>
      <div class="app-container">
      <el-card shadow="never">
 <!--  头部搜索模块 -->
          <el-form>
           <el-form-item label="学科名称">
            <el-input v-model="int" size="small" style="width: 200px;" ></el-input>
            <el-button style="margin-left:10px" size="small" @click="eliminateAdd">清除</el-button>
            <el-button type="primary" size="small" @click="SearchBoxAdd" :loading="isloading">搜索</el-button>
            <el-button class="filter-item fr" @click="addRolee" type="success" icon="el-icon-edit" size="small">新增学科</el-button>
           </el-form-item>
        </el-form>
            <el-alert
          v-if="alertText !== ''"
          :title="'一共'+ this.total+'条'"
          type="info"
          class="alert"
          :closable="false"
          show-icon
        ></el-alert>
    <el-table
    :data="list"
    style="width: 100%">
    <el-table-column
      fixed
      type="index"
      label="序号"
      width="150">
      <!-- <template slot-scope="scope">
          {{scope.row}}
      </template> -->
    </el-table-column>
    <el-table-column
      prop="subjectName"
      label="学科名称"
      width="120">
    </el-table-column>
    <el-table-column
      prop="username"
      label="创建者"
      width="120">
    </el-table-column>
    <el-table-column
      prop="addDate"
      label="创建日期"
      width="120">
    </el-table-column>
    <el-table-column
      prop="isFrontDisplay"
      label="前台是否显示"
      width="200">
         <template slot-scope="scope">
         <span v-if="scope.row.isFrontDisplay===1" >是</span>
         <span v-else>否</span>
      </template>
    </el-table-column>
    <el-table-column
      prop="twoLevelDirectory"
      label="二级目录"
      width="150">
    </el-table-column>
      <el-table-column
      prop="tags"
      label="标签"
      width="220">
    </el-table-column>
      <el-table-column
      prop="twoLevelDirectory"
      label="题目数量"
      width="220">
    </el-table-column>
      <el-table-column
      prop="zip"
      label="操作">
      <template #default="scope">
          <el-button type="text" @click="disciPlineadd(scope.row.id)">学科分类</el-button>
       <el-button type="text" @click="labelsAdd(scope.row.id)">学科标签</el-button>
        <el-button type="text" @click="redacAdd(scope.row.id,scope.row)">修改</el-button>
       <el-button type="text" @click="deleteadd(scope.row.id)">删除</el-button>
      </template>
    </el-table-column>
  </el-table>
   <div class="block">
    <el-pagination
    class="paging"
    @size-change="handleSizeChange"
    @current-change="change"
    :current-page="params.page"
     :page-sizes="[5,10,15]"
      :page-size="params.pagesize"
       background
      layout="total, sizes, prev, pager, next, jumper"
      :total="total">
    </el-pagination>
  </div>
  </el-card>
    </div>
<!-- 新增弹框 -->
         <el-dialog
          title="新增学科"
          :visible="showDialog"
           @close="closeDialog"
          width="30%">
          <el-form ref="form" :model="form" :rules="rules">
            <el-form-item label="学科名称" prop="subjectName">
              <el-input v-model="form.subjectName"  style="width: 200px;" placeholder="请输入内容"></el-input>
            </el-form-item>
          </el-form>
          <el-form>
            <el-form-item label="是否显示">
              <el-switch
                style="margin-left: 5px;"
                v-model="form.isFrontDisplay"
                active-color="#13ce66"
                inactive-color="#ff4949">
              </el-switch>
            </el-form-item>
          </el-form>
          <span slot="footer" class="dialog-footer">
            <el-button   @click="showDialog=false">取 消</el-button>
            <el-button v-if="addOrmod" type="primary" @click="btnok">确 定</el-button>
            <el-button v-else type="primary" @click="modBtn">确 定</el-button>
          </span>
        </el-dialog>
  </div>
</template>

<script>
import { list, remove, update, add } from '@/api/hmmm/subjects'
export default {
  data () {
    return {
      isloading: false,
      int: null, // 搜索框
      queInp: '', // 查询框
      list: [], // 所有数据
      tablezx: window.innerHeight - 300 + 'px',
      total: 0, // 总条数
      showDialog: false,
      swiValue: true, // 在当前页面是否显示
      //   isFrontDisplay: 1,
      alertText: '',
      addOrmod: true,
      theId: 0, // 记录当前操作id
      curPage: '',
      testpage: [],
      curId: '',
      rules: {
        subjectName: [
          { required: true, message: '请输入科目名', trigger: 'blur' }
        ]
      },
      form: {
        subjectName: '',
        isFrontDisplay: true
      },
      params: {
        page: 1,
        pagesize: 5
      }
    }
  },
  mounted () {
    this.getList()
  },
  methods: {
    // 获取页面列表
    async  getList () {
      list({
        ...this.params,
        subjectName: ''
      }).then(res => {
        this.total = res.data.counts // 获取总条数
        console.log(res)
        this.list = res.data.items
        this.total = res.data.counts
      })
    },
    // 打开新增弹框
    addRolee () {
      this.showDialog = true
    },
    // 关闭新增弹框
    closeDialog () {
      this.showDialog = false
    },
    // 删除
    deleteadd (id) {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        // 调用删除接口
        await remove({ id })
        // 更新列表
        this.getList()
        // 提示用户
        this.$message.success('删除角色成功')
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
      //   this.showDialog = false
    },
    // 跳转学科分类
    disciPlineadd (row) {
      this.$router.push('/subjects/directorys?id=' + row.id + '&name=' + row.subjectName)
    },
    // 跳转学科标签
    labelsAdd (row) {
      this.$router.push('/subjects/tags?id=' + row.id + '&name=' + row.subjectName)
    },
    // 修改
    redacAdd (row, val) {
      this.showDialog = true
      this.addOrmod = false
      this.dialogVisible = true
      console.log(val)
      this.form.subjectName = val.subjectName
      this.form.isFrontDisplay = val.isFrontDisplay === 1
      row.isFrontDisplay === 1 ? this.swiValue = true : this.swiValue = false
      this.theId = row
    },
    async modBtn () {
      this.swiValue ? this.isFrontDisplay = 1 : this.isFrontDisplay = 0
      this.$refs.form.validate(async (valid) => {
        if (valid) {
          try {
            this.form.isFrontDisplay = this.form.isFrontDisplay ? 1 : 0
            await update({
              id: this.theId,
              subjectName: this.form.subjectName,
              isFrontDisplay: 1
            })
            this.$message.success('修改成功')
            this.form = {
              subjectName: '',
              isFrontDisplay: true
            }
            this.showDialog = false
            this.getList()
          } catch (err) {
            this.$message.error('修改失败')
          }
        } else {
          return false
        }
      })
    },
    // 清除搜索框
    eliminateAdd () {
      this.int = ''
    },
    // 搜索框
    async SearchBoxAdd () {
    //   console.log(123)
      const res = await list({
        subjectName: this.int,
        page: 1,
        pagesize: 10

      })
      //   console.log(res)
      this.list = res.data.items
    },
    // 新增确定
    btnok () {
      this.$refs.form.validate(async validate => {
        if (validate) {
          await add(this.form)
          // 重新拉取数据
          this.getList()
          this.$message.success('操作成功')
          this.showDialog = false
        }
        this.form.subjectName = ''
      })
    },
    // 分页
    handleSizeChange (val) { // 每页/条
      console.log(val)
      this.params.pagesize = val
      this.getList()
    },
    change (newPage) {
      this.params.page = newPage
      this.getList()
    }
  }
}
</script>

<style scoped >
.app-container {
  padding: 20px 20px 0 20px;
}
.paging {
    float: right;
}
</style>
